package com.ruoyi.production.controller;

import java.io.InputStream;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.production.domain.ProductManagement;
import com.ruoyi.production.service.IProductManagementService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 产品列表Controller
 * 
 * @author admin
 * @date 2024-08-30
 */
@RestController
@RequestMapping("/production/management")
public class ProductManagementController extends BaseController
{
    @Autowired
    private IProductManagementService productManagementService;

    /**
     * 查询产品列表列表
     */
    @PreAuthorize("@ss.hasPermi('production:management:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductManagement productManagement)
    {
        startPage();
        List<ProductManagement> list = productManagementService.selectProductManagementList(productManagement);
        return getDataTable(list);
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<ProductManagement> util = new ExcelUtil<ProductManagement>(ProductManagement.class);
        util.importTemplateExcel(response, "产品列数据");
    }
    /**
     * 导入数据
     */
    @Log(title = "产品列", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('production:management:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<ProductManagement> util = new ExcelUtil<ProductManagement>(ProductManagement.class);
        InputStream inputStream = file.getInputStream();
        List<ProductManagement> list = util.importExcel(inputStream );
        inputStream.close();
        int count = productManagementService.batchInsertProductManagement(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }


    /**
     * 导出产品列表列表
     */
    @PreAuthorize("@ss.hasPermi('production:management:export')")
    @Log(title = "产品列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductManagement productManagement)
    {
        List<ProductManagement> list = productManagementService.selectProductManagementList(productManagement);
        ExcelUtil<ProductManagement> util = new ExcelUtil<ProductManagement>(ProductManagement.class);
        util.exportExcel(response, list, "产品列表数据");
    }

    /**
     * 获取产品列表详细信息
     */
    @PreAuthorize("@ss.hasPermi('production:management:query')")
    @GetMapping(value = "/{productId}")
    public AjaxResult getInfo(@PathVariable("productId") Long productId)
    {
        return success(productManagementService.selectProductManagementByProductId(productId));
    }

    /**
     * 新增产品列表
     */
    @PreAuthorize("@ss.hasPermi('production:management:add')")
    @Log(title = "产品列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductManagement productManagement)
    {
        return toAjax(productManagementService.insertProductManagement(productManagement));
    }

    /**
     * 修改产品列表
     */
    @PreAuthorize("@ss.hasPermi('production:management:edit')")
    @Log(title = "产品列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductManagement productManagement)
    {
        return toAjax(productManagementService.updateProductManagement(productManagement));
    }

    /**
     * 删除产品列表
     */
    @PreAuthorize("@ss.hasPermi('production:management:remove')")
    @Log(title = "产品列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{productIds}")
    public AjaxResult remove(@PathVariable Long[] productIds)
    {
        return toAjax(productManagementService.deleteProductManagementByProductIds(productIds));
    }
}
